package com.adong.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @author com.adong
 */
@Configuration //配置类
@ComponentScan("com.adong") //组件扫描，相当于   <context:component-scan base-package="com.adong"></context:component-scan>
@PropertySource("classpath:jdbc.properties")
//加载jdbc配置文件，相当于<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
public class ConfigTest {
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("jdbc.password")
    private String password;

    @Bean("dataSource") //Spring会将该方法的返回值以指定名称存入到Spring容器中
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

}
